﻿using System;
using System.Linq;
using System.Text;
using Infrastructure;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;


namespace OpenAuth.App
{
    public class YIN_MZJHJLKApp : TriageBaseApp<YIN_MZJHJLK>
    {


        /// <summary>
        /// 加载列表
        /// </summary>
        //public TableData Load(QueryYIN_MZJHJLKListReq request)
        //{
        //    var loginContext = _auth.GetCurrentUser();
        //    if (loginContext == null)
        //    {
        //        throw new CommonException("登录已过期", Define.INVALID_TOKEN);
        //    }

        //    var properties = loginContext.GetProperties("YIN_MZJHJLK");

        //    if (properties == null || properties.Count == 0)
        //    {
        //        throw new Exception("当前登录用户没有访问该模块字段的权限，请联系管理员配置");
        //    }


        //    var result = new TableData();
        //    var objs = Repository.Find(null);
        //    if (!string.IsNullOrEmpty(request.key))
        //    {
        //        objs = objs.Where(u => u.Id.Contains(request.key));
        //    }


        //    var propertyStr = string.Join(',', properties.Select(u => u.Key));
        //    result.columnHeaders = properties;
        //    result.data = objs.OrderBy(u => u.Id)
        //        .Skip((request.page - 1) * request.limit)
        //        .Take(request.limit).Select($"new ({propertyStr})");
        //    result.count = objs.Count();
        //    return result;
        //}

        public void Add(AddOrUpdateYIN_MZJHJLKReq req)
        {
            var obj = req.MapTo<YIN_MZJHJLK>();
            //todo:补充或调整自己需要的字段
            //obj.CreateTime = DateTime.Now;
            //var user = _auth.GetCurrentUser().User;
            //obj.CreateUserId = user.Id;
            //obj.CreateUserName = user.Name;
            Repository.Add(obj);
        }
        /// <summary>
        /// 向叫号表中插入数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(YIN_MZJHJLK model)
        {
            Repository.Add(model);
        }
        /// <summary>
        /// 向叫号表中插入数据
        /// </summary>
        /// <param name="model">本地表中得叫号demo</param>
        public void AddSign(TriageSign model)
        {
            YIN_MZJHJLK yin = new YIN_MZJHJLK();
            yin.GHXH = model.GHXH;
            yin.JSSJH = model.JSSJH;
            yin.PATID = model.PATID;
            yin.BLH = model.BLH;
            yin.YBDM = model.YBDM;
            yin.CARDNO = model.CARDNO;
            yin.YYXH = model.YYXH;
            yin.HZXM = model.HZXM;
            yin.GHRQ = model.GHRQ;
            yin.KSDM = model.KSDM;
            yin.KSMC = model.KSMC;
            yin.YSDM = model.YSDM;
            yin.YSMC = model.YSMC;
            yin.GHHX = model.GHHX;
            yin.GHLB = model.GHLB;
            yin.YYXH = model.YYXH;
            yin.IPDZ = model.IPDZ;
            yin.ZJDM = model.ZJDM;
            yin.ZJMC = model.ZJMC;
            yin.ZQDM = model.ZQDM;
            yin.SCJHSJ = model.SCJHSJ;
            yin.ZHJHSJ = DateTime.Now.ToString("yyyyMMddhh:mm:ss");
            yin.JHCS = 1;
            yin.ZHJHSJ2 = DateTime.Now.ToLocalTime();
            yin.JLZT = 0;
            Add(yin);
        }
        /// <summary>
        /// 更新叫号表中得叫号次数
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="GHXH">挂号序号</param>
        /// <param name="jhcs">叫号次数</param>
        public void Update(string patid, string GHXH, int jhcs)
        {
            string date = DateTime.Now.ToString("yyyyMMdd");
            string cc = DateTime.Now.ToString("yyyyMMddHH:mm:ss");
            Repository.Update(u => u.PATID.ToString("0") == patid && u.GHXH==int.Parse(GHXH), u => new YIN_MZJHJLK
            {
                ZHJHSJ = DateTime.Now.ToString("yyyyMMddHH:mm:ss"),
                ZHJHSJ2 = DateTime.Now.ToLocalTime(),
                JHCS = jhcs + 1,
                JLZT = 0
            });
        }
        /// <summary>
        /// 获取叫号表中得对象
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="GHXH">挂号序号</param>
        /// <returns></returns>
        public YIN_MZJHJLK QuerySingle(string patid, string GHXH)
        {
            return Repository.FindSingle(u => u.PATID.ToString("0") == patid && u.GHXH.ToString("0") == GHXH);
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="app"></param>
        /// <param name="auth"></param>
        public YIN_MZJHJLKApp(ITriageRepository<YIN_MZJHJLK> repository
            ) : base(repository)
        {

        }
    }
}